# Copyright (C) 2001 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#
# ${tool}_check_compile -- Reports and returns pass/fail for a compilation
#

proc ${tool}_check_compile {testcase option objname gcc_output} {
    global tool
    set fatal_signal "*cc: Internal compiler error: program*got fatal signal"
 
    if [string match "$fatal_signal 6" $gcc_output] then {
	${tool}_fail $testcase "Got Signal 6, $option"
	return 0
    }

    if [string match "$fatal_signal 11" $gcc_output] then {
	${tool}_fail $testcase "Got Signal 11, $option"
	return 0
    }

    # We shouldn't get these because of -w, but just in case.
    if [string match "*cc:*warning:*" $gcc_output] then {
	warning "$testcase: (with warnings) $option"
	send_log "$gcc_output\n"
	unresolved "$testcase, $option"
	return 0
    }

    set gcc_output [prune_warnings $gcc_output]

    set unsupported_message [${tool}_check_unsupported_p $gcc_output]
    if { $unsupported_message != "" } {
	unsupported "$testcase: $unsupported_message"
	return 0
    }

    # remove any leftover LF/CR to make sure any output is legit
    regsub -all -- "\[\r\n\]*" $gcc_output "" gcc_output

    # If any message remains, we fail.
    if ![string match "" $gcc_output] then {
	${tool}_fail $testcase $option
	return 0
    }

    # fail if the desired object file doesn't exist.
    # FIXME: there's no way of checking for existence on a remote host.
    if {$objname != "" && ![is3way] && ![file exists $objname]} {
	${tool}_fail $testcase $option
	return 0
    }

    ${tool}_pass $testcase $option
    return 1
}

#
# ${tool}_pass -- utility to record a testcase passed
#

proc ${tool}_pass { testcase cflags } {
    if { "$cflags" == "" } {
	pass "$testcase"
    } else {
	pass "$testcase, $cflags"
    }
}

#
# ${tool}_fail -- utility to record a testcase failed
#

proc ${tool}_fail { testcase cflags } {
    if { "$cflags" == "" } {
	fail "$testcase"
    } else {
	fail "$testcase, $cflags"
    }
}

#
# ${tool}_finish -- called at the end of every script that calls ${tool}_init
#
# Hide all quirks of the testing environment from the testsuites.  Also
# undo anything that ${tool}_init did that needs undoing.
#

proc ${tool}_finish { } {
    # The testing harness apparently requires this.
    global errorInfo;

    if [info exists errorInfo] then {
	unset errorInfo
    }

    # Might as well reset these (keeps our caller from wondering whether
    # s/he has to or not).
    global prms_id bug_id
    set prms_id 0
    set bug_id 0
}

#
# ${tool}_exit -- Does final cleanup when testing is complete
#

proc ${tool}_exit { } {
    global gluefile;

    if [info exists gluefile] {
	file_on_build delete $gluefile;
	unset gluefile;
    }
}
    
#
# ${tool}_check_unsupported_p -- Check the compiler(/assembler/linker) output 
#	for text indicating that the testcase should be marked as "unsupported"
#
# Utility used by mike-gcc.exp and c-torture.exp.
# When dealing with a large number of tests, it's difficult to weed out the
# ones that are too big for a particular cpu (eg: 16 bit with a small amount
# of memory).  There are various ways to deal with this.  Here's one.
# Fortunately, all of the cases where this is likely to happen will be using
# gld so we can tell what the error text will look like.
#

proc ${tool}_check_unsupported_p { output } {
    if [regexp "(^|\n)\[^\n\]*: region \[^\n\]* is full" $output] {
	return "memory full"
    }
    return ""
}

#
# runtest_file_p -- Provide a definition for older dejagnu releases
# 		    and assume the old syntax: foo1.exp bar1.c foo2.exp bar2.c.
# 		    (delete after next dejagnu release).
#

if { [info procs runtest_file_p] == "" } then {
    proc runtest_file_p { runtests testcase } {
	if { $runtests != "" && [regexp "\[.\]\[cC\]" $runtests] } then {
	    if { [lsearch $runtests [file tail $testcase]] >= 0 } then {
		return 1
	    } else {
		return 0
	    }
	}
	return 1
    }
}
